package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.dto.CommonDTO;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.SmMemberTeacherApprovalNew;
import com.shycloud.mido.member.api.vo.SmMemberTeacherApprovalNewVO;
import com.shycloud.mido.member.mapper.TeacherApprovalMapper;
import com.shycloud.mido.member.service.MemberServiceV2;
import com.shycloud.mido.member.service.SmMemberTeacherApprovalNewService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


/**
 * 平台认证教师 controller
 *
 * @author nianhua.jiang
 * @date 2019-02-27 13:16:46
 */
@RestController
@AllArgsConstructor
@RequestMapping("/teacherapproval")
@ApiIgnore
public class TeacherApprovalController {

	/** 平台认证教师 service */
	private final SmMemberTeacherApprovalNewService teacherApprovalService;
	private TeacherApprovalMapper teacherApprovalMapper;

	/** 会员信息 service */
	private final MemberServiceV2 memberService;
	/** 审核状态：已驳回 */
	private final String STATUS_APPROVAL_REJECT = "2";

	/** 七牛云 OSS */
	private final MinioTemplate minioTemplate;

	/**
	 * 分页获取教师信息
	 *
	 * @param current
	 * @param size
	 * @param name
	 * @param status  1->已审核
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/19 17:57
	 */
	@GetMapping("/page")
	@ApiOperation("/分页获取教师信息")
	public R getTeacherApprovalPage(Integer current, Integer size, String name, String status, String optId) {

		//分页参数
		Page page = new Page(current, size);
		return new R<>(teacherApprovalService.getTeacherApprovalPage(page, name, status, optId));
	}

	@GetMapping("/getTeacherPage")
	public R getTeacherPage(Integer current, Integer size, String name, String optId) {
		Page page = new Page(current, size);
		return new R<>(teacherApprovalService.getTeacherApprovalPage(page, name, "0", optId));
	}

	@GetMapping("/getTeacherStatistics")
	public R getTeacherStatistics(Integer optId) {

		Map<String, String> map = new HashMap();
		String teacher0Num = "0";
		String teacher1Num = "0";

		teacher0Num = teacherApprovalMapper.getTeacher0Num(optId);
		teacher1Num = teacherApprovalMapper.getTeacher1Num(optId);

		map.put("teacher0Num", teacher0Num);
		map.put("teacher1Num", teacher1Num);
		return new R<>(map);
	}

	/**
	 * 通过id查询平台认证教师表
	 *
	 * @param id id
	 * @return R
	 */
	@GetMapping("/getById")
	public R getById(Integer id) {

		//通过ID查询教师详细的认证信息
		SmMemberTeacherApprovalNewVO vo = teacherApprovalService.getTeacherDetail(id);
		//将【教学成果】的字符串分割成数组
		if (vo.getAchievementPic() != null) {
			String[] arr = vo.getAchievementPic().split(",");
			vo.setImgList(arr);
		}
		return new R(vo);
	}


	/**
	 * 上传封面图片
	 *
	 * @param imgArr 图片URL
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/4/30 9:26
	 */
	@PostMapping("uploadImage")
	@ApiOperation(value = "上传教学成果展示")
	public R uploadImage(@RequestParam("imgArr[]") List<MultipartFile> imgArr) {

		List<Map<String, String>> resultList = new ArrayList<>();

		for (MultipartFile img : imgArr) {

			//提取后缀
			//提取图片名称
			String fileName = img.getOriginalFilename().replace(" ", "");
			String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

			String uuid = UUID.randomUUID().toString();
			//提取图片名称
			String imgName = uuid.replace("-", "") + "." + suffix;
			//初始化map结果集
			Map<String, String> resultMap = new HashMap<>(4);
			//名称
			resultMap.put("name", imgName);
			try {
				//上传操作
				resultMap.put("url", minioTemplate.uploadFile(img.getInputStream(), imgName));
				resultList.add(resultMap);
			} catch (Exception e) {
				return R.builder().code(CommonConstants.FAIL).msg(e.getLocalizedMessage()).build();
			}

		}
		return R.builder().data(resultList).build();
	}

	/**
	 * 新增平台认证教师表
	 *
	 * @param smMemberTeacherApprovalNewVO 平台认证教师表
	 * @return R
	 */
	@PostMapping("saveTeacherApproval")
	public R save(@RequestBody SmMemberTeacherApprovalNewVO smMemberTeacherApprovalNewVO) {
		return new R(teacherApprovalService.saveTeacherApproval(smMemberTeacherApprovalNewVO));
	}

	/**
	 * 通过审核
	 * <p>
	 * //	 * @param paramsJson paramsJson
	 *
	 * @param ids 审核ID数组
	 * @return R
	 */
	@PostMapping("/accept")
	public R accept(@RequestBody Integer[] ids) {
		if (teacherApprovalService.accept(ids)) {
			return new R<>(true, "审核成功");
		} else {
			return new R<>(false, "审核失败");
		}
	}

	/**
	 * 驳回审核
	 *
	 * @param
	 * @return R
	 */
	@PostMapping("/reject")
	public R reject(@RequestBody CommonDTO commonDTO) {

		//查询要驳回的数据
		SmMemberTeacherApprovalNew teacherApprovalNew = teacherApprovalService.getById(commonDTO.getId());
		//修改状态为【已驳回】
		teacherApprovalNew.setStatus(STATUS_APPROVAL_REJECT);
		//发送推送告知认证失败
		teacherApprovalService.sendMsg(teacherApprovalNew.getMemberId());
		//执行操作
		return new R(teacherApprovalService.updateById(teacherApprovalNew));

	}

	/**
	 * 查询此用户是否为认证教师
	 *
	 * @param
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/6/16 18:15
	 */
	@GetMapping("checkTeacherApproval")
	@ApiOperation("查询此用户是否为认证教师")
	public R checkTeacherApproval() {

		//获取member_id
		String member_id = SecurityUtils.getMember().getId();

		return new R(teacherApprovalService.checkTeacherApproval(member_id));
	}

	@GetMapping("addApproval")
	@ApiOperation("新增教师认证")
	public R addApproval(String memberId) {

		Member member = memberService.getById(memberId);

		//新增教师认证表
		SmMemberTeacherApprovalNew teacherApproval = new SmMemberTeacherApprovalNewVO();
		//状态：已通过
		teacherApproval.setStatus("1");
		//电话号码
		teacherApproval.setPhone(member.getPhone());
		//用户ID
		teacherApproval.setMemberId(member.getId());
		//教师姓名
		teacherApproval.setName(member.getShowname());

		return new R(teacherApprovalService.save(teacherApproval));
	}

}